import 'dart:convert';

import 'package:ch_core_utils/ch_core_utils.dart';

enum SplashType { video, picture }

enum SplashEventType { pullup, click, shake }

class SplashEntity {
  // enum
  SplashType type;
  // enum
  SplashEventType eventType;
  String sourceUrl;
  String eventAction;
  SplashEntity({
    required this.type,
    required this.eventType,
    required this.sourceUrl,
    required this.eventAction,
  });

  SplashEntity copyWith({
    SplashType? type,
    SplashEventType? eventType,
    String? sourceUrl,
    String? eventAction,
  }) {
    return SplashEntity(
      type: type ?? this.type,
      eventType: eventType ?? this.eventType,
      sourceUrl: sourceUrl ?? this.sourceUrl,
      eventAction: eventAction ?? this.eventAction,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'type': type.index,
      'eventType': eventType.index,
      'sourceUrl': sourceUrl,
      'eventAction': eventAction,
    };
  }

  factory SplashEntity.fromMap(Map<String, dynamic> map) {
    return SplashEntity(
      type: SplashType.values[map['type'] ?? 0],
      eventType: SplashEventType.values[map['eventType'] ?? 0],
      sourceUrl: map.getString('sourceUrl') ?? '',
      eventAction: map.getString('eventAction') ?? '',
    );
  }

  String toJson() => json.encode(toMap());

  factory SplashEntity.fromJson(String source) =>
      SplashEntity.fromMap(json.decode(source));

  @override
  String toString() {
    return 'SplashEntity(type: $type, eventType: $eventType, sourceUrl: $sourceUrl, eventAction: $eventAction)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is SplashEntity &&
        other.type == type &&
        other.eventType == eventType &&
        other.sourceUrl == sourceUrl &&
        other.eventAction == eventAction;
  }

  @override
  int get hashCode {
    return type.hashCode ^
        eventType.hashCode ^
        sourceUrl.hashCode ^
        eventAction.hashCode;
  }
}
